c  This program calculates the difference between two model arrays
c  The arrays can be either formatted or unformatted.  for unformatted
c  arrays, the file is read according to unformatted arrays output
c  by the McDonald-Harbaugh 3d ground-water model.
c
c  written for dg by Lynette Brooks
c  September 1991
c  Utah District
c  Water Resources
c
c  declare variables 
c
      real array1(200,200),array2(200,200),diff(200,200),inact
      dimension text(4)    !text needed to read unformatted arrays
      character*132 ifile1,ifile2,ofile
      character*32 ifmt1,ifmt2,ofmt
      integer row,col
c
c  prompt for data file names
c
      write(6,*)' enter name of file containing first array'
      read(5,'(a)')ifile1
      write(6,*)' enter name of file containing second array'
      read(5,'(a)')ifile2
c
c  open and read first file
c
      WRITE (6,*) 'ENTER FORMAT OF first data FILE: Example (10f8.2)',
     1          'if unformatted enter UNF'
      READ (5,'(a32)') IFMT1
      if (ifmt1.eq.'unf'.or.ifmt1.eq.'UNF') then
         open (unit=7,file=ifile1,status='old',form='unformatted')
         write(6,*)'enter time step, period, and layer wanted'
         read(5,*)istp,iper,ilay
 10      read (7) KSTP,KPER,PERTIM,TOTIM,TEXT,COL,ROW,KLAY
         read(7)((array1(i,j),j=1,col),i=1,row)
         if ((kstp.ne.istp).or.(kper.ne.iper).or.(klay.ne.ilay)) then
            go to 10
         end if
         rewind(7)     !in case it is also used for second array
      else
         OPEN (UNIT=7,FILE=INFILE,STATUS='OLD')
         write(6,*)' enter number of rows,columns'
         READ(5,*)row,col
         do 12 i = 1,row
            READ(7,IFMT1)(ARRAY1(row,col),J=1,COL)
 12      continue
      end if
c
c  open and read second file
c
      WRITE (6,*) 'ENTER FORMAT OF second data FILE: Example (10f8.2)',
     1          'if unformatted enter UNF'
      READ (5,'(a32)') IFMT2
      if (ifmt2.eq.'unf'.or.ifmt2.eq.'UNF') then
         open (unit=8,file=ifile2,status='old',form='unformatted')
         rewind (8)
         write(6,*)'enter time step, period, and layer wanted'
         read(5,*)istp,iper,ilay
 20      read (8) KSTP,KPER,PERTIM,TOTIM,TEXT,COL,ROW,KLAY
         read(8)((array2(i,j),j=1,col),i=1,row)
         if ((kstp.ne.istp).or.(kper.ne.iper).or.(klay.ne.ilay)) then
            go to 20
         end if
         rewind(8)
      else
         OPEN (UNIT=8,FILE=INFILE,STATUS='OLD')
         write(6,*)' enter number of rows,columns'
         READ(5,*)row,col
         do 22 i = 1,row
            READ(8,IFMT2)(ARRAY2(row,col),J=1,COL)
 22      continue
      end if
c
c  calculate difference between arrays
c
      write(6,*)' enter the number used for inactive cells',
     1           'Difference will be 0 at inactive cells'
      read(5,*)inact
      do 30 i = 1,row
         do 40 j = 1,col
            diff(i,j)=array1(i,j)-array2(i,j)
            if ((array1(i,j).eq.inact).or.(array2(i,j).eq.inact)) then
               diff(i,j) = 0
            end if
 40      continue
 30   continue
c
c  open output file and write output
c
      write(6,*)' enter name of output file'
      read(5,'(a)')ofile
      WRITE (6,*) 'ENTER FORMAT OF output FILE: Example (10f8.2)',
     1          'if unformatted enter UNF'
      READ (5,'(a32)') ofmt
      if (ofmt.eq.'unf'.or.ofmt.eq.'UNF') then
         open (unit=9,file=ofile,status='old',form='unformatted')
c
c  if both input files were formatted, then need to assign pertim,totim,text
c  and read layer,row, and column
c
         if (ifmt1.ne.'unf'.or.ifmt1.ne.'UNF') then
            if (ifmt2.ne.'unf'.or.ifmt2.ne.'UNF') then
               pertim = 999.
               totim = 999.
               text(1)='data from formatted files'
               text(2)=''
               text(3)=''
               text(4)=''
               write(6,*)'enter time step,period, and layer'
               read(5,*)istp,iper,ilay
            end if
         end if
         write (9) ISTP,IPER,PERTIM,TOTIM,TEXT,COL,ROW,ILAY
         write (9)((diff(i,j),j=1,col),i=1,row)
      else
         open(unit=9,file=ofile,status='unknown')
         do 50 i=1,row
            write(9,ofmt)(diff(i,j),j=1,col)
 50      continue
      end if
c
c  close files and end
      rewind (9)
      close (7)
      close (8)
      stop
      end


